<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <link rel="stylesheet" href="css/login.css" media="all">
    <link rel="stylesheet" href="css/common.css" media="all">
</head>
<body>
<div class="user-login user-display-show user-login-bg" id="user-login-container" style="display: none;">
    <div class="user-login-main ">
        <div class="user-login-box user-login-header">
            <h2>BASE4WEB</h2>
            <p>XXXX后台管理系统</p>
        </div>
        <form class="user-login-box layui-form">
            <div class="layui-form-item">
                <label class="user-login-icon layui-icon layui-icon-username" for="login-name"></label>
                <input type="text" name="loginName" id="login-name" lay-verify="required" placeholder="登录名"
                       class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="user-login-icon layui-icon layui-icon-password" for="login-password"></label>
                <input type="password" name="password" id="login-password" lay-verify="required" placeholder="密码"
                       class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="user-login-icon layui-icon layui-icon-vercode" for="login-vercode"></label>
                        <input type="text" name="vercode" id="login-vercode" lay-verify="required" placeholder="图形验证码"
                               class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="/captcha/kaptcha.jpg" onclick="chageCode()" title="图片看不清？点击重新得到验证码"
                                 class="user-login-codeimg" id="vercode">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <input name="rememberMe" lay-skin="primary" lay-filter="rememberCheckBox" id="login-remember"
                       title="记住密码" type="checkbox" value="1">
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login-submit" id="loginBtn">登录</button>
            </div>
        </form>
    </div>
</div>

<script src="layui/layui.js"></script>
<script src="js/common.js"></script>
<script src="js/md5.js"></script>
<script>
    //刷新验证码
    function chageCode() {
        document.getElementById("vercode").src = "/captcha/kaptcha.jpg?" + Math.random();
    }
    //扩展cookie模块
    layui.config({
        base: '/js/' //存放拓展模块的根目录
    }).extend({ //设定模块别名
        jquery_cookie: 'jquery.cookie'
    }).use(['form', 'jquery_cookie'], function () {
        var $ = layui.$
        , form = layui.form
        , layer = layui.layer
        , $ = layui.jquery_cookie($);//这里是给jquery加一个方法，并不是替换

        var checkedRemembe; //记住我是否选中标示

        //取cookie值并给输入框赋值
        $('#login-name').val($.cookie('loginName'));
        $('#login-password').val($.cookie('password'));
        if ($.cookie('loginName')) {
            checkedRemembe = true;
            $('#login-remember').attr("checked", true);
        }

        //渲染表单,使赋值生效
        form.render();

        form.on('checkbox(rememberCheckBox)', function (data) {
            if (data.elem.checked) {
                checkedRemembe = true;
            } else {
                checkedRemembe = false;
            }
        });

        //提交
        form.on('submit(login-submit)', function (obj) {
            var field = obj.field;

            //输入的原始密码
            var originalPass = field.password;

            //Md5加密
            field.password = hex_md5(field.password);

            //按钮不用，防止重复提交
            $('#loginBtn').attr('disabled',true);

            //提交登录请求
            doAjax($, layer, 'POST', false, field, '/dologin', afterLogin);

            //登录成功回调函数
            function afterLogin(result) {
                var data = result.data;

                if (result.status == '200') {
                    //选择记住密码则保存登录信息到cookie,7天
                    if (checkedRemembe) {
                        $.cookie('loginName', field.loginName, {expires: 7});
                        $.cookie('password', originalPass, {expires: 7});
                    } else {
                        $.cookie('loginName', '');
                        $.cookie('password', '');
                    }

                    layui.data('userData',{
                        key:'data'
                        ,value:data
                    })

                    //登入成功的提示与跳转
                    layer.msg('登录成功', {
                        icon: 1
                        , time: 2000
                    }, function () {
                        location.href = '/index'; //后台主页
                    });
                } else {
                    layer.msg(result.msg);
                    //按钮可用
                    $('#loginBtn').attr('disabled',false);
                }
            }

            return false;
        });


    });
</script>
</body>
</html>